package com.janlei.acc.builder;

/**
 * 建造者模式
 */
public class BuilderStu {
    /*
    1. product 具体的产品 (可能有多个，最好继承一个抽象类，便于统计信息)
    2. builder 定义创建product的方法
    3. concrete builder 实现builder接口，具体实现创建product
    4. director 构架一个使用builder接口，用于创建一个复杂的对象
     */
}

/**
 * 产品模板 abstract product
 */
abstract class House {
    String house;
}

/**
 * 实际产品A product
 */
class CommonHouse extends House {
    private final String house;

    public CommonHouse(String house) {
        this.house = house;
    }

    @Override
    public String toString() {
        return "CommonHouse{" +
                "house='" + house + '\'' +
                '}';
    }
}

/**
 * 实际产品B product
 */
class HighBuilding extends House {

    private final String house;

    public HighBuilding(String house) {
        this.house = house;
    }

    @Override
    public String toString() {
        return "HighBuilding{" +
                "house='" + house + '\'' +
                '}';
    }
}

/**
 * 建房方法接口 builder
 */
interface IBuilderHouse {
    void buildBasic();

    void buildWalls();

    void roofed();
}

/**
 * 实际生产产品操作 concrete builder
 */
class BuilderHouse implements IBuilderHouse {

    private House house;//注入一个产品

    public void setHouse(House house) {
        this.house = house;
    }

    @Override
    public void buildBasic() {
        System.out.println(String.format("%s正在%s", house, "buildBasic"));
    }

    @Override
    public void buildWalls() {
        System.out.println(String.format("%s正在%s", house, "buildWalls"));
    }

    @Override
    public void roofed() {
        System.out.println(String.format("%s正在%s", house, "roofed"));
        System.out.println(String.format("%s修建完成", house));
        System.out.println();
    }
}

/**
 * 产品加工工厂 director
 */
class BuilderHouseF {
    public static void builder(IBuilderHouse iHouse) {
        iHouse.buildBasic();
        iHouse.buildWalls();
        iHouse.roofed();
    }
}